package com.jfirer.redisserver;

import com.jfirer.baseutil.TRACEID;
import com.jfirer.jnet.common.api.ProcessorContext;
import com.jfirer.jnet.common.api.ReadProcessor;
import com.jfirer.redisserver.command.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CommandHandler implements ReadProcessor<Command>
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CommandHandler.class);

    private final RedisCore redisCore;

    public CommandHandler(RedisCore redisCore)
    {
        this.redisCore = redisCore;
    }

    @Override
    public void read(Command command, ProcessorContext ctx)
    {
        String traceId = TRACEID.currentTraceId();
        LOGGER.debug("traceId:{} 本次处理的命令：{}", traceId, command.type().name());
        command.handle(ctx, redisCore);
        LOGGER.debug("traceId:{} 命令处理完毕", traceId);
    }

    @Override
    public void exceptionCatch(Throwable e, ProcessorContext ctx)
    {
        LOGGER.error("traceId:{} 出现异常", TRACEID.currentTraceId(), e);
        ctx.fireExceptionCatch(e);
    }
}
